<template>
	<view class="page">
		<view class="head">
			<Search @search="search" ref="search"></Search>
			<view>
				<AdImagesSwiper :adImagesList="adsImg"></AdImagesSwiper>
			</view>
		</view>
		<view class="body">
			<view class="information" >
				<view style="height: 100rpx;" v-if="screenMenuIsShow">
					<view class="information-class" @click="screenMenuIsFixed" :class="screenIsFixed">
						<Screen :screenList="screenList" @change="screenSelectItem"></Screen>
					</view>
				</view>
				<view class="infomation-list">
					<InformationItem :infoList="infoList" :isMyPage="false" :isDel="false" :isMenu="true"></InformationItem>
				</view>
			</view>
		</view>
		<view class="bottem">
			<!-- <BackHome v-if="backTopValue"></BackHome> -->
			<BackHome :backTopIsShow="backTopValue"></BackHome>
		</view>
	</view>
</template>

<script>
import Search from "@/components/Search/Search.vue"
import InformationItem from "@/components/InformationItem/InformationItem.vue"
import Screen from "@/components/Screen/Screen.vue"
import BackHome from "@/components/FloatButton/BackHome.vue"
import AdImagesSwiper from "@/components/AdImagesSwiper/AdImagesSwiper.vue"
	export default {
		data() {
			return {
				adsImg:[],
				infoClassid:0,
				infoClassType:0,
				infoList:[],
				// tempscreenList:[], //临时存储下拉菜单对象数据
				screenList:[],//下拉菜单对象
				screenIsFixed: '', //下拉是否固定
				params:{},
				currentPage: 1, // 默认加载第二页
				allPage: 1, // 所有页面
				backTopValue: false, // 判断是否显示悬浮返顶按钮
				screenMenuIsShow:true ,//当分类为0时不显示下拉筛选菜单
				defaultParams:{}, //设置默认的参数对象
				searchKey: '', // 搜索关键字
			}
		},
		async onLoad(option) {  //option为object类型，会序列化上个页面传递的参数
			await this.$onLaunched;
			this.params = JSON.parse(decodeURIComponent(option.parameter)); //打印出上个页面传递的参数。
			this.infoClassId = this.params.ID
			this.infoClassType = this.params.Type
			if(this.infoClassType == 0){
				this.screenMenuIsShow = false
			}
			this.defaultParams = {cid:getApp().globalData.cityId,id:this.infoClassId,type:this.infoClassType,openid:getApp().globalData.openid}
			let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage})
			this.getColumnInfo(paramsObject)
		},
		onPageScroll(e){
			if(e.scrollTop>205){
				this.screenIsFixed = 'is-fixed'
				this.backTopValue = true
			}else{
				this.screenIsFixed = '';
				this.backTopValue = false
			}
		},
		onReachBottom(){
			this.currentPage++
			if(this.currentPage <= this.allPage){
				if(this.searchKey){
					let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage, search: this.searchKey})
					this.searchMore(paramsObject)
				} else {
					let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage})
					this.getColumnInfoListMore(paramsObject)
				}
				
			}
		},
		onPullDownRefresh(){
			this.screenList = [] //让子组件重新加载数据
			this.infoList = [] //让子组件重新加载数据
			this.currentPage = 1 // 默认加载第二页
			this.allPage = 1 // 所有页面
			this.searchKey = '' // 设置关键字为空
			this.$refs.search.searchKey = '' // 设置子组件关键字为空
			if(this.infoClassType == 0){
				this.screenMenuIsShow = false
			}
			let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage})
			this.getColumnInfo(paramsObject)
			// this.$forceUpdate()
		},
		methods: {
			screenSelectItem(e){
			    console.log(e)
				this.currentPage = 1
				this.allPage = 1
				let paramsObject = {cid:getApp().globalData.cityId,id:this.infoClassId,type:this.infoClassType,openid:getApp().globalData.openid,page:this.currentPage}
				e.forEach(el=>{
					paramsObject = Object.assign(paramsObject, JSON.parse('{"'+el.classId+'": '+el.ID+'}'))
				})
				console.log(paramsObject)
				this.infoList = []
				this.defaultParams = paramsObject
				// this.tempscreenList = []
				// this.screenList = []//下拉菜单对象
				this.getColumnInfoListMore(paramsObject)
			},			
			getColumnInfo(params){
				this.onLoading(1200)
				this.$api.getColumnInfoList(params)
				.then(res =>{
					console.log(res.data)
					this.adsImg = res.data.AdsImg
					this.infoList = res.data.XXList
					this.currentPage = res.data.CurrentPage
					this.allPage = res.data.Allpage// 所有页面
					uni.setNavigationBarTitle({
					    title: res.data.LmName
					});
					// 重要提示
					// for(let key in res.data){
					// 	console.log(key)
					// 	console.log(res.data[key])
					// }
					// console.log(res.data['XlList1'])
					let tempscreenList = []
					if(this.infoClassType == 1){
						tempscreenList.push({classId:'XLID1',className:res.data['XlName'],data:res.data['XlList']})
					}else{
						for(let i=1;i<=this.infoClassType;i++)
						{
							tempscreenList.push({classId:'XLID'+ i,className:res.data['XlName'+i],data:res.data['XlList'+i]})
						}
					}
					this.screenList = tempscreenList
					
					this.$forceUpdate()
					uni.stopPullDownRefresh()
				})
			},
			getColumnInfoListMore(params){
				// this.onLoading(1500)
				this.$api.getColumnInfoListMore(params)
				.then(res =>{
					this.infoList = [...this.infoList, ...res.data.XXList]
					this.currentPage = res.data.CurrentPage
					this.allPage = res.data.Allpage// 所有页面
					this.$forceUpdate()
				})
				
			},
			screenMenuIsFixed(){
				uni.pageScrollTo({
				    duration:0,
				    scrollTop:205
				})
			},
			onLoading(time){
				uni.showToast({
				  title: "加载中...",
				  icon: "loading",
				  duration: time,
				})
			},
			search(key){
				this.searchKey = key
				let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage, search: this.searchKey})
				this.currentPage = 1
				this.allPage = 1
				this.infoList = []
				this.$api.getSearchColumnInfoList(paramsObject).then(res => {
					console.log(res)
					this.infoList = [...this.infoList, ...res.data.XXList]
					this.currentPage = res.data.CurrentPage
					this.allPage = res.data.Allpage// 所有页面
					this.$forceUpdate()
				})
			},
			searchMore(params){
				
				this.$api.getSearchColumnInfoList(params).then(res => {
					console.log(res)
					this.infoList = [...this.infoList, ...res.data.XXList]
					this.currentPage = res.data.CurrentPage
					this.allPage = res.data.Allpage// 所有页面
					this.$forceUpdate()
				})
			},
		},
		components:{
			Search,
			InformationItem,
			Screen,
			BackHome,
			AdImagesSwiper,
		}
	}
</script>

<style scoped>
@import url("infoList.css");
</style>
